package cn.itcast.mr.partition;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * Author itcast
 * Date 2021/10/17 10:07
 * Desc 将每行的数据转换成 <彩票号,整行数据>
 * 彩票号根据 \t 切分出来的
 * LongWritable : 代表当前内容的偏移量  KEYIN
 * Text : 每行的数据   VALUEIN
 * IntWritable : 彩票号   KEYOUT
 * Text : 每行的数据  VALUEOUT
 */
public class CaipiaoMapper extends Mapper<LongWritable,Text, IntWritable, Text> {
    /**
     *
     * @param key 输入的偏移量
     * @param value  输入的每行数据
     * @param context  处理的上下文
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        //判空
        if(StringUtils.isNotEmpty(line)){
            String[] caipiaoArr = line.split("\t");
            String caipiaoCode = caipiaoArr[5];
            if(StringUtils.isNotEmpty(caipiaoCode)){
                context.write(new IntWritable(Integer.parseInt(caipiaoCode)),value);
            }
        }
    }
}
